package com.bjfu.demo.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.util.CollectionUtils;

/**
 * Created on 2021-04-30
 */
public class AvgUtils {


    public static Double getAvgFromDouble(List<Double> values) {
        if (CollectionUtils.isEmpty(values)) {
            return 0D;
        }


        Double sum = 0D;
        for (Double value : values) {
            sum += value;
        }

        return sum / values.size();

    }

    public static Double getAvgFromString(List<String> values) {

        values = values.parallelStream().filter(x -> (null!=x)&&(!x.contains("-"))).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(values)) {
            return 0D;
        }




        List<Double> valuess = new ArrayList<>();


        for (String value : values) {

            try {
                double v = Double.parseDouble(value);
                valuess.add(v);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return getAvgFromDouble(valuess);

    }
}
